<?php
	if(have_posts()){
		
	if(is_search()){
		global $wp_query;		
		$total_results = $wp_query->found_posts;
		$total_pages = $wp_query->max_num_pages;
		$search= esc_attr( apply_filters( 'the_search_query', get_search_query(false)));				
		
		$message = sprintf( __('%s search results for <span class="search_term">\'%s\'</span>'), $total_results, $search);
		
		?>
		<div class="textbox search_results">
			<?php echo $message; ?>
		</div>
		<?php
	}
		
	?>	
	
	

	<div nav_page="<?php echo cl_nav_get_page_num(); ?>">

	<?php
		while ( have_posts() ) : the_post();
			
			## PREVIEW ##
			if(cl_post_is_preview()){
		
				switch (get_post_type()){
					case 'cl_gallery':
						cl_template('clgallery-preview');
						break;
						break;
					case 'post':
					default:
						cl_template('post-preview');
						break;	
				}
				
			}			
			else
			## FULL ##
			{
				switch (get_post_type()){
					case 'cl_gallery':
						cl_template('clgallery');
						break;
					case 'attachment':
						if(wp_attachment_is_image())
							cl_template('image');
						else
							cl_template('attachment'); 	
						break;
					case 'post':
					default:
						cl_template('post');
						break;	
				}
			}
						
		endwhile;
		
		$num_pages_loaded= (cl_count_posts() / cl_nav_get_posts_per_page());
		$last_loaded_page= cl_nav_get_page_num() + $num_pages_loaded - 1;
		$next_page_link= get_pagenum_link($last_loaded_page+1);
	?>

<?php	
		
		if(!is_singular() && strlen(get_next_posts_link())>0){ ?>
		<nav class="cl_nav">
			<div class="textcenter cl_link">
				<?php echo is_int($last_loaded_page)?'<a href="'.$next_page_link.'" class="box small cl_next">'.__('Load More Posts').'</a>':''; ?>				
			</div>
			<div class="textcenter cl_loading hide">
				<img src="<?php bloginfo('template_url')?>/img/icon/posts-loader-desktop.gif" class="post_loader_desktop" />
				<img src="<?php bloginfo('template_url')?>/img/icon/posts-loader-devices.gif" class="post_loader_devices" />
			</div>
		</nav>
				
		<?php } ?>
		
	</div>
<?php }	?>
